package com.google.explorecode.mamat.servimagesend;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.Date;

/**
 *
 * @author mamat-dev
 */
public class ImageServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		InputStream in = request.getInputStream();
		BufferedReader r = new BufferedReader(new InputStreamReader(in));
		StringBuffer buf = new StringBuffer();
		String line;
		while ((line = r.readLine()) != null) {
			buf.append(line);
		}
		String s = buf.toString();
		// write a file
		FileOutputStream f1;
		File directory = new File("D:/Image/");
	    	directory.mkdirs();
        String nameFile = ""+new Date().getTime();
	   	String path = "D:/Image/"+nameFile+".png";
		byte[] data = Base64.decode(s);
		try {
			f1 = new FileOutputStream(path);
			f1.write(data);
			f1.close();
		} catch (IOException e) {
			System.out.println("Problems creating the file");
		}
        
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
		doPost(req, resp);
	}

}
